#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <linux/rtc.h>
#include <linux/i2c.h>  
#include <linux/i2c-dev.h>   

#define BCD_TO_DEC(val) (((val)&15) + ((val)>>4)*10)
#define DEC_TO_BCD(val)	((((val)/10)<<4) + (val)%10)

static int iic_read(int fd, char buff[], int addr, int count)  
{  
	int ret;  

	write(fd, &addr, 1);        
	ret = read(fd,buff,count);  

	printf("read %d byte at 0x%x\n", ret, addr); 
	
	return ret;  
}  

static int iic_write(int fd, char buff[], int addr, int count)  
{  
	int ret;  
	int i, n;  
	static char sendbuffer[100];  

	memcpy(sendbuffer+1, buff, count);  

	sendbuffer[0] = addr;  

	ret = write(fd, sendbuffer, count+1);  

	printf("write %d byte at 0x%x\n", ret, addr);  
}  

void pt7c43390_bin_swap(unsigned char * s){
    unsigned char temp1,temp2,temp3;
    temp1=((*s)&0x55)<<1;
    temp2=((*s)&0xaa)>>1;
    temp3=(temp1|temp2);
    temp1=(temp3&0x33)<<2;
    temp2=(temp3&0xcc)>>2;
    temp3=(temp1|temp2);
    temp1=(temp3&0x0f)<<4;
    temp2=(temp3&0xf0)>>4;
    (*s)=(temp1|temp2);
}

int main(int argc, char **argv)
{
	int ret = -1;
	int fd = -1;
	int i = 0;
	const char *dev_name = "/dev/i2c-3";
	unsigned char slave = 0x00;
	unsigned char reg = 0x00;
	unsigned int count = 0x00;
	unsigned char buff[128] = {0x00};
	int y,n,d,w,h,m,s;

	static int once = 1;

	printf("rtc-debug [slave address] [reg] [read count]...\n");

	fd = open(dev_name, O_RDWR);
	if (fd < 0) {
		printf("open %s failed\n", dev_name);
		return -1;
	}

	slave = atoi(argv[1]);
	printf("salve address = 0x%02x.\n", slave);

	reg = atoi(argv[2]);
	printf("reg = 0x%02x.\n", reg);

	count = atoi(argv[3]);
	printf("count = 0x%02x.\n", count);

	ret = ioctl(fd, I2C_TIMEOUT, 2);		/* 设置超时时间 */
	ret = ioctl(fd, I2C_RETRIES, 1);		/* 设置重发次数 */
	ret = ioctl(fd,I2C_TENBIT,0);			/* not 10bit */
	ret = ioctl(fd,I2C_SLAVE,slave);		/* 设置I2C从设备地址[6:0] */
	printf(" ret = %d\n", ret);

	if (once == 1) {
		buff[0] = 0x00;
		buff[1] = 0x00;
		buff[2] = 0x00;
		buff[3] = 0x00;
		buff[4] = 0x00;
		buff[5] = 0x00;
		buff[6] = 0x00;
		iic_write(fd, buff, reg, 7);  

		once = 0;

		sleep(1);
	}

	while (1) {

		buff[0]=0xFF;  
		buff[1]=0xFF;  
		buff[2]=0xFF;  
		buff[3]=0xFF;  
		buff[4]=0xFF;  
		printf("read i2c .....\n");  
		ret = iic_read(fd, buff, reg, count);  
		printf("%d bytes read:", ret);  

		pt7c43390_bin_swap(&buff[0]);//年
		pt7c43390_bin_swap(&buff[1]);//月
		pt7c43390_bin_swap(&buff[2]);//日
		pt7c43390_bin_swap(&buff[3]);//周
		pt7c43390_bin_swap(&buff[4]);//时
		pt7c43390_bin_swap(&buff[5]);//分
		pt7c43390_bin_swap(&buff[6]);//秒

		s = BCD_TO_DEC(buff[0x06] & 0x7F);
		m = BCD_TO_DEC(buff[0x05] & 0x7F);
		h = BCD_TO_DEC(buff[0x04] & 0x3F);
		w = BCD_TO_DEC(buff[0x03] & 0x3F);
		d = BCD_TO_DEC(buff[0x02] & 0x07);
		n = BCD_TO_DEC(buff[0x01] & 0x1F);
		n -= 1;
		y = BCD_TO_DEC(buff[0x00] & 0xFF);

		printf("%4d-%02d-%02d(%d) %02d:%02d:%02d\n",
				1900 + y, n, d,
				w, h, m, s);

		for(i = 0; i < ret; i++){  
			printf("0x%02x ", buff[i]);  
		}  

		printf("\n");  
		sleep(1);
	}

	return 0;
}

